package com.mall.db.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsHomeAdvertiseMapper;
import com.mall.pojo.SmsHomeAdvertise;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class SmsHomeAdvertiseApiImpl implements SmsHomeAdvertiseApi{

    @Autowired
    private SmsHomeAdvertiseMapper homeAdvertiseMapper;

    /**
     * 查询首页广告分页信息
     * @param pageNum
     * @param pageSize
     * @param name
     * @param endTime
     * @param type
     * @return
     */
    @Override
    public Page<SmsHomeAdvertise> list(Integer pageNum, Integer pageSize, String name, String endTime, Integer type) {
        Page<SmsHomeAdvertise> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeAdvertise> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(name),SmsHomeAdvertise::getName,name)
                .eq(ObjectUtil.isNotEmpty(endTime),SmsHomeAdvertise::getEndTime,endTime)
                .eq(ObjectUtil.isNotEmpty(type),SmsHomeAdvertise::getType,type)
                .orderByDesc(SmsHomeAdvertise::getSort);
        homeAdvertiseMapper.selectPage(page,wrapper);
        return page;
    }

    /**
     * 根据id查询首页广告信息
     * @param id
     * @return
     */
    @Override
    public SmsHomeAdvertise findById(Integer id) {
        return homeAdvertiseMapper.selectById(id);
    }

    /**
     * 修改首页广告信息
     * @param advertise
     * @return
     */
    @Override
    public Integer update(SmsHomeAdvertise advertise) {
        return homeAdvertiseMapper.updateById(advertise);
    }

    /**
     * 添加广告
     * @param advertise
     * @return
     */
    @Override
    public Integer create(SmsHomeAdvertise advertise) {
        return homeAdvertiseMapper.insert(advertise);
    }

    /**
     * 通过广告url查询广告信息
     * @param url
     * @return
     */
    @Override
    public SmsHomeAdvertise findByURL(String url) {
        LambdaQueryWrapper<SmsHomeAdvertise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsHomeAdvertise::getUrl,url);
        return homeAdvertiseMapper.selectOne(wrapper);
    }

    /**
     * 批量删除广告
     * @param ids
     * @return
     */
    @Override
    public Integer deleteByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        return homeAdvertiseMapper.deleteBatchIds(idList);
    }
}
